package com.team.web;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.IdeaCommentDao;
import com.team.dao.UserDao;
import com.team.entity.IdeaComment;
import com.team.entity.User;
import com.team.util.Const;
import com.team.util.TUtil;

public class IdeaCommentServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method=request.getParameter("_m");
		if("save".equals(method)){
			save(request,response);
		}else if("select".equals(method)){
			selectAll(request,response);
		}
		
	}
	private void selectAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String ideaId = request.getParameter("ideaId");
		int id=Integer.parseInt(ideaId);
		
		IdeaCommentDao dao= new IdeaCommentDao();
		List<IdeaComment> listIdeaComment=dao.selectByIdeaId(id);
		
		UserDao ud=new UserDao();
		
		if(listIdeaComment != null){
			Map<IdeaComment,User> map = new HashMap<IdeaComment,User>();
			for(IdeaComment ideaComment: listIdeaComment){
				User temp = ud.selectById(ideaComment.getUserId());
				if(temp != null){
					map.put(ideaComment, temp);
				}else{
					System.out.println("temp find error");
				}
			}
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_IDEACOMMENTLIST, listIdeaComment);
			session.setAttribute(Const.SESSION_IDEACOMMENTMAP, map);
			response.sendRedirect("ideashow.jsp");
			return;
		}else{
			response.sendRedirect("idea.jsp");
			return;
		}
	}
	private void save(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		
		String id = request.getParameter("ideaId");
		int ideaId = Integer.parseInt(id); 
		String content = request.getParameter("content");
		System.out.println("ideaId:"+ideaId);
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		if(content != null && content != ""){
			IdeaComment ideaComment = new IdeaComment();
			
			ideaComment.setContent(content);
			ideaComment.setCreateTime(TUtil.getNow());
			ideaComment.setIdeaId(ideaId);
			ideaComment.setUserId(user.getId());
			
			IdeaCommentDao dao = new IdeaCommentDao();
			int num =dao.save(ideaComment);
			if(num > 0){
				System.out.println("save is succ");
				response.sendRedirect("ideaComment.jspx?_m=select&ideaId="+ideaId);
				return;
			}else{
				System.out.println("save is error");
			}
		}else{
			response.sendRedirect("idea.jsp");
		}
		
	}
}
